export interface DataFactory {
    encode(data: any): string;

    decode(data: string): object;
}

export class JsonDataFactory implements DataFactory {
    decode(data: string): object {
        return JSON.parse(data);
    }

    encode(data: any): string {
        return JSON.stringify(data || {});
    }
}

declare const require: any;
const pako = require('../../third-scripts/gzip/pako.min');

// gzip
export class GzipDataFactory implements DataFactory {

    decode(data: string): object {
        const obj = JSON.parse(pako.inflate(data, {to: 'string'}));
        // console.log("解析消息：", data.length, obj);
        return obj;
    }

    encode(data: any): string {
        const str = JSON.stringify(data);
        const binaryString = pako.deflate(str, {to: 'string'});
        // console.log("压缩消息：", str.length, binaryString.length, binaryString);
        return binaryString;
    }
}